<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$party           = array();
$bullionDetail   = array();
$opening         = array();
$total           = array();
$bulliondate     = 0;
//$partyId         = isset($_REQUEST['party']) ? $_REQUEST['party'] : 0;
$totNetWeight    = 0;
$bulliondetailid = 0;
$billBookNo      = 0;
$billNo          = 0;
$showPrev        = 0;
$fromDate        = isset($_POST['fromDateYear']) ? $_POST['fromDateYear']."-".$_POST['fromDateMonth']."-".$_POST['fromDateDay'] : date("Y-m-d");
$toDate          = isset($_POST['toDateYear']) ? $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'] : date("Y-m-d");
$partyName       = "";
$bulliondetail   = "";
$bullionmasterid = isset($_POST['party']) ? $_POST['party'] : 0;
$total['kg']       = 0;
$total['cramount'] = 0;
$total['crfine']   = 0;
$total['dramount'] = 0;
$total['totalAmt'] = 0;

$opening['particulars']     = "Opening";
$opening['debit']           = 0;
$opening['credit']          = 0;
$opening['vadhaaroGhatado'] = 0;
$opening['qty']             = 0;
$opening['rate']            = 0;
$opening['crAmount']        = 0;
$opening['drAmount']        = 0;
$opening['totalAmt']        = 0;
$b                          = 0;
$netfine                    = 0;
$netamount                  = 0;
$fromDateSelected           = "";

if(!isset($_REQUEST['party']) && !isset($_REQUEST['partyId']))
  $partyId = 0;
else
  $partyId = isset($_REQUEST['party']) ? $_REQUEST['party'] : $_GET['partyId'] ;
   
 
  
  if(isset($_POST['party']) && $_POST['party'] > 0)
  {
  $selectbullionDetail = "SELECT bulliondetail.bulliondetailid,bullionmaster.bullionmasterid,bullionmaster.partyId,bulliondetail.bullionmasterid,DATE_FORMAT(bullionmaster.bulliondate,'%Y-%m-%d') AS bulliondate,bulliondetail.particulars,bulliondetail.kg,bulliondetail.bhav,bulliondetail.cramount,bulliondetail.crfine,bulliondetail.dramount
                            FROM bulliondetail
                            JOIN bullionmaster ON bulliondetail.bullionmasterid = bullionmaster.bullionmasterid
                           WHERE bullionmaster.partyId = ".$_POST['party']."
                             AND bullionmaster.bulliondate >= '".$fromDate."'
                             AND bullionmaster.bulliondate <= '".$toDate."'
                           ORDER BY bullionmaster.bulliondate, bulliondetail.bullionmasterid";
  $selectbullionDetailRes = mysql_query($selectbullionDetail);
  $bullionDetailRow = mysql_num_rows($selectbullionDetailRes);
  while($bullionDetailRow = mysql_fetch_array($selectbullionDetailRes))
  {
    $bullionDetail[$b]['partyId'] = $bullionDetailRow['partyId'];
    $bullionDetail[$b]['bulliondetailid'] = $bullionDetailRow['bulliondetailid'];
    $bullionDetail[$b]['bullionmasterid'] = $bullionDetailRow['bullionmasterid'];
    $bullionDetail[$b]['bulliondate']     = $bullionDetailRow['bulliondate'];
    $bullionDetail[$b]['particulars']     = $bullionDetailRow['particulars'];
    $bullionDetail[$b]['kg']              = $bullionDetailRow['kg'];
    $bullionDetail[$b]['bhav']            = $bullionDetailRow['bhav'];
    $bullionDetail[$b]['cramount']        = $bullionDetailRow['cramount'];
    $bullionDetail[$b]['crfine']          = $bullionDetailRow['crfine'];
    $bullionDetail[$b]['dramount']        = $bullionDetailRow['dramount'];
    $b++;
    
    $total['kg']       += $bullionDetailRow['kg'];
    $total['cramount'] += $bullionDetailRow['cramount'];
    $total['crfine']   += $bullionDetailRow['crfine'];
    $total['dramount'] += $bullionDetailRow['dramount'];
    $total['totalAmt'] += $bullionDetailRow['dramount'] - $bullionDetailRow['cramount'];
  }
   $total['netfine']   = ($total['kg'] - $total['crfine']);
   $total['netamount'] = ($total['dramount'] - $total['cramount']);
}

$selectParty = "SELECT partyId,bullionPartyName,bullionFromDate 
                  FROM  bullionparty
                 ORDER BY bullionPartyName DESC";
$selectPartyRes = mysql_query($selectParty);
$a = 0;
$party['partyId'][$a]   = 0;
$party['bullionPartyName'][$a] = "Select";
$a++;
while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
  $party['partyId'][$a]   = $partyRow['partyId'];
  $party['bullionPartyName'][$a] = $partyRow['bullionPartyName'];
  $party['bullionFromDate'][$a] = $partyRow['bullionFromDate'];
  $a++;
  if(isset($_POST['bdate']) == "1" && $partyRow['partyId'] == $_POST['party'])
  {
    $fromDateSelected = $partyRow['bullionFromDate'];
  }
}
$smarty = new Smarty;
require("bottom.php");
$smarty->assign("l_bullionDetail",$l_bullionDetail);
$smarty->assign("l_partySelect",$l_partySelect);
$smarty->assign("l_from",$l_from);
$smarty->assign("l_to",$l_to);
$smarty->assign("l_bulliondate",$l_bulliondate);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_kg",$l_kg);
$smarty->assign("l_bhav",$l_bhav);
$smarty->assign("l_cramount",$l_cramount);
$smarty->assign("l_crfine",$l_crfine);
$smarty->assign("l_dramount",$l_dramount);
$smarty->assign("netfine",$netfine);
$smarty->assign("netamount",$netamount);
$smarty->assign("l_delete",$l_delete);
$smarty->assign("bullionmasterid",$bullionmasterid);
$smarty->assign("bulliondetail",$bulliondetail);
$smarty->assign("bulliondetailid",$bulliondetailid);
$smarty->assign("total",$total);
$smarty->assign("fromDate",$fromDate);
$smarty->assign("bullionDetail",$bullionDetail);
$smarty->assign("total",$total);

$smarty->assign("party",$party);
$smarty->assign("partyId",$partyId);
$smarty->assign("toDate",$toDate);
$smarty->assign("l_find",$l_find);
$smarty->assign("l_bullionPrint",$l_bullionPrint);
$smarty->assign("fromDateSelected",$fromDateSelected);
$smarty->display("bullionDetail.tpl");
?>